﻿using Project.API.Models;
using Project.BLL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Project.API.Controllers
{
    public class CartDetailController : ApiController
    {
        CartDetailBll bll = new CartDetailBll();

        // GET: api/CartDetail
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/CartDetail/5
        public int Get(int id)
        {
            int count = bll.ShuLian(id);
            return count;
        }

        [Route("Api/CartDetail/CX")]
        public List<CartDetailDto> GetCX(int id)
        {
            List<milktea_cart_detail> data = bll.details(id);

            var list = from d in data
                       select new CartDetailDto
                       {
                           cdid = d.cart_detail_id,
                           gid = d.Goods_Id,
                           gname = d.Goods.name,
                           cid = d.Cart_id,
                           price = d.price,
                           quantity = d.quantity,
                           specification = d.specification
                       };

            return list.ToList();
        }

        // POST: api/CartDetail
        public Result Post([FromBody]CartDetailDto value)
        {
            milktea_cart_detail detail = new milktea_cart_detail();
            detail.cart_detail_id = value.cdid;
            detail.Goods_Id = value.gid;
            detail.Cart_id = value.cid;
            detail.price = value.price;
            detail.quantity = value.quantity;
            detail.specification = value.specification;

            bll.CartJiaGoods(detail);

            bool zt = bll.CartDetailAdd(detail);

            Result result = new Result();
            result.code = zt ? 1 : 0;
            result.msg = zt ? "加入成功": "加入失败";

            return result;

        }

        // PUT: api/CartDetail/5
        public int Put(int sl, int cid,int gid)
        {
            bool zt = bll.UpdateSL(sl, cid, gid);

            int count = 0;

            if (bll.Query().Where(d => d.Cart_id == cid).Count() > 0) {
                count = bll.Query().Where(d => d.Cart_id == cid).Sum(d => d.quantity);
            }

            return count;

        }

        // DELETE: api/CartDetail/5
        public void Delete(int id)
        {
            
        }

    }
}
